package com.kingint.web.listener.serviceOrder;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kingint.mbg.ser.entity.SerReceiveWorkMiddle;
import com.kingint.mbg.ser.entity.SerServiceOrder;
import com.kingint.mbg.ser.entity.SerServiceOrderMiddle;
import com.kingint.mbg.ser.entity.SerWorkOrder;
import com.kingint.mbg.ser.mapper.SerServiceOrderMapper;
import com.kingint.mbg.ser.mapper.SerServiceOrderMiddleMapper;
import com.kingint.mbg.ser.mapper.SerWorkOrderMapper;
import com.kingint.mbg.sys.entity.UmsMember;
import com.kingint.web.admin.serve.service.SerReceiveWorkMiddleService;
import com.kingint.web.portal.base.service.WorkOrderTypeService;
import com.kingint.web.sys.service.UmsAdminService;
import com.kingint.web.util.WorkType;
import com.kingint.web.util.spring.SpringContextHolder;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
import org.activiti.engine.runtime.ProcessInstance;

import java.util.Date;

public class OrderReceiveListener implements TaskListener {
    @Override
    public void notify(DelegateTask delegateTask) {
        SerWorkOrderMapper orderMapper = SpringContextHolder.getBean(SerWorkOrderMapper.class);
        String processInstanceId = delegateTask.getProcessInstanceId();//流程实例id
        String eventName = delegateTask.getEventName();//获取事件名称
        if (eventName.equals("create")) {
            QueryWrapper<SerWorkOrder> wrapper = new QueryWrapper<>();
            wrapper.eq("process_instance_id", processInstanceId);
            SerWorkOrder workOrder = new SerWorkOrder();
            workOrder.setOrderStatus(2);//处理中
            workOrder.setStartTime(new Date());//开始处理时间
            orderMapper.update(workOrder, wrapper);//更新相关额状态
        }
        if (eventName.equals("complete")) {
            String assignee = delegateTask.getAssignee();
            UmsAdminService adminService = SpringContextHolder.getBean(UmsAdminService.class);
            UmsMember adminByName = adminService.getAdminByName(assignee);//用户信息

            String rejectStatus = delegateTask.getVariable("completeStatus").toString();
            int status = 0;
            if (rejectStatus != null && rejectStatus.equals("1")) {//完成
                //判断完成是否超时
                status = 5;
            } else if (rejectStatus != null && rejectStatus.equals("2")) {
                status = 4;//挂起
            }
            //查询业务相关信息
            RuntimeService runtimeService = SpringContextHolder.getBean(RuntimeService.class);
            ProcessInstance instance = runtimeService.createProcessInstanceQuery()
                    .processInstanceId(processInstanceId)
                    .singleResult();
            String businessKey = instance.getBusinessKey();//业务相关信息

            String[] s = businessKey.split("_");
            Integer integer = Integer.valueOf(s[2]);//workId
            SerWorkOrder serWorkOrder = new SerWorkOrder();
            serWorkOrder.setId(integer);
            serWorkOrder.setOrderStatus(status);
            if(status==5){
                serWorkOrder.setEndTime(new Date());//完成时间
            }
            orderMapper.updateById(serWorkOrder);//更新相关额状态

            //查询中间表修改当前用户状态 挂起时
            if (status == 4) {
                //修改中间表的信息
                SerReceiveWorkMiddleService middleService = SpringContextHolder.getBean(SerReceiveWorkMiddleService.class);
                SerReceiveWorkMiddle workMiddle = new SerReceiveWorkMiddle();
                workMiddle.setWorkId(integer);
                workMiddle.setStatus(1);
                workMiddle.setIsReplace(1);//更换处理人
                workMiddle.setReceiveUserId(adminByName.getId());
                middleService.updateReceiveWorkInfo(workMiddle);

                //修改服务单接单状态
                SerServiceOrderMapper serviceOrderMapper = SpringContextHolder.getBean(SerServiceOrderMapper.class);
                QueryWrapper<SerServiceOrder> wrapper = new QueryWrapper<>();
                wrapper.eq("work_id",integer);
                SerServiceOrder serviceOrder = serviceOrderMapper.selectOne(wrapper);
                QueryWrapper<SerServiceOrderMiddle> queryWrapper = new QueryWrapper<>();
                queryWrapper.eq("service_order_id",serviceOrder.getId())
                        .eq("handle_user_id",adminByName.getId());

                SerServiceOrderMiddle orderMiddle = new SerServiceOrderMiddle();
                orderMiddle.setStatus(1);
                SerServiceOrderMiddleMapper orderMiddleMapper = SpringContextHolder.getBean(SerServiceOrderMiddleMapper.class);
                orderMiddleMapper.update(orderMiddle,queryWrapper);
            }
        }
    }
}
